using UnityEngine;

public class Skill_Base : MonoBehaviour
{
    [HideInInspector] public Player player;
    [HideInInspector] public PlayerSkillManager skillManager;

    [Header("-----General Details-----")]
    [SerializeField] protected SkillType skillType;
    [SerializeField] protected SkillUpgradeType skillUpgradeType;
    public DamageScaleData damageScaleData;
    [SerializeField] protected float cooldown;
    private float lastTimeUsed;

    protected virtual void Awake()
    {
        player = GetComponentInParent<Player>();
        skillManager = GetComponentInParent<PlayerSkillManager>();
        lastTimeUsed -= cooldown;
    }
    public virtual bool CanUseSkill()
    {
        if (skillUpgradeType == SkillUpgradeType.None)
            return false;
        if (IsCoolDown())
            return false;
        return true;
    }

    //判断是否处于冷却状态
    protected bool IsCoolDown() => Time.time < lastTimeUsed + cooldown;

    //记录上一次释放技能的时间（重置技能计时）
    public void SetSkillOnCoolDown()
    {
        lastTimeUsed = Time.time;
        player.uI.inGameUI.GetSkillSlotInSkillType(skillType)?.StartCooldownInSlot();
    }
    public void ReduceCooldownBy(float cooldownReduction)
    {
        lastTimeUsed += cooldownReduction;
        player.uI.inGameUI.GetSkillSlotInSkillType(skillType)?.UpdateCooldownReducing(cooldownReduction);
    }
    public void ResetCoolDown(float coolDown) => lastTimeUsed = Time.time - coolDown;

    //判断技能是否解锁
    protected bool Unlock(SkillUpgradeType skillUpgradeType) => this.skillUpgradeType == skillUpgradeType;

    public void SetSkillUpgrade(Skill_DataSO skillData, bool isReset = false)
    {
        UpgradeData skillUpgradeData = skillData.skillUpgradeData;
        player.uI.inGameUI.GetSkillSlotInSkillType(skillData.skillType)?.SetSkillSlot(skillData);

        if (isReset)
        {
            skillUpgradeType = SkillUpgradeType.None;
            cooldown = 0;
            damageScaleData = null;
            return;
        }
        skillUpgradeType = skillUpgradeData.skillUpgradeType;
        cooldown = skillUpgradeData.coolDown;
        damageScaleData = skillUpgradeData.damageScaleData;
        ResetCoolDown(cooldown);
    }
    public virtual void TryUseSkill()
    {

    }
    public SkillType GetSkillType() => skillType;
    public SkillUpgradeType GetSkillUpgradeType() => skillUpgradeType;

}

